package com.example.CarFee;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.trainfee.R;

public class BuyActivity extends Activity {
	private Button confirmBtn, cancelBtn;
	private ImageView backBtn;
	private String startPlace, destination;
	private TextView nameText, phoneText, placeText, dateText, timeText;
	private String start;
	private String end;
	private String phone;
	private String date;
	private String time;
	private String station;
	private ProgressDialog bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buy);
		Intent intent = getIntent();
		startPlace = intent.getStringExtra("start");
		destination = intent.getStringExtra("end");
		date = intent.getStringExtra("date");
		time = intent.getStringExtra("time");
		station = intent.getStringExtra("station");
		nameText = (TextView)findViewById(R.id.name);
		phoneText = (TextView)findViewById(R.id.phone);
		placeText = (TextView)findViewById(R.id.place);
		timeText = (TextView)findViewById(R.id.time);
		dateText = (TextView)findViewById(R.id.date);
		dateText.setText(date);
		timeText.setText(time);
		nameText.setText(Info.name);
		phoneText.setText(Info.phone);
		placeText.setText(startPlace+"至"+destination);
		backBtn = (ImageView) findViewById(R.id.backBtn);
		confirmBtn = (Button) findViewById(R.id.confirmBtn);
		cancelBtn = (Button) findViewById(R.id.cancelBtn);
		bar = new ProgressDialog(this);
		bar.setTitle("预定中");
		bar.setMessage("请稍后...");
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		confirmBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				bar.show();
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							start = URLEncoder.encode(startPlace, "UTF-8");
							end = URLEncoder.encode(destination, "UTF-8");
							station = URLEncoder.encode(station, "UTF-8");
							date = URLEncoder.encode(date, "UTF-8");
							time = URLEncoder.encode(time, "UTF-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						if (Service.BuyCarTicket(start, end, station, date,
								time)) {
							handler.sendEmptyMessage(0x120);
						} else {
							handler.sendEmptyMessage(0x121);
						}
					}
				});
				thread.start();
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Toast.makeText(BuyActivity.this, "取消订票成功",
				// Toast.LENGTH_SHORT).show();
				finish();
			}
		});

	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			bar.cancel();
			if (msg.what == 0x120) {
				Toast.makeText(BuyActivity.this, "预订成功,可以在您的订单情况查看",
						Toast.LENGTH_SHORT).show();
				finish();
			}
			if (msg.what == 0x121) {
				Toast.makeText(BuyActivity.this, "预订失败", Toast.LENGTH_SHORT)
						.show();
			}
		}
	};
}
